﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using WINADM.Server;


namespace WINADM.Controller
{
    public partial class ControllerService : ServiceBase
    {
        Timer HeartBeatUpdateTimer = new Timer(120000);
        Timer NetworkInterfaceInformationUpdateTimer = new Timer(1000);
        WINADM.Server.Controller _controller = new WINADM.Server.Controller();
        public ControllerService()
        {
            InitializeComponent();
            log4net.Config.XmlConfigurator.Configure();
            HeartBeatUpdateTimer.Elapsed += new ElapsedEventHandler(HeartBeatUpdateTimer_Elapsed);
            NetworkInterfaceInformationUpdateTimer.Elapsed += new ElapsedEventHandler(NetworkInterfaceInformationUpdateTimer_Elapsed);
            
        }

        void NetworkInterfaceInformationUpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _controller.UpdateComputerPerformanceStats();

        }

        void HeartBeatUpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            UpdateSystemInfo();
        }

        private void UpdateSystemInfo()
        {
            _controller.RegisterComputer();
            _controller.UpdateNetworkInterfaceInformation();
            _controller.UpdateProcessInformation();
            _controller.UpdateServiceInformation();
            _controller.UpdateEventLogEntries();
            _controller.UpdateDriveInformation();
        }

        protected override void OnStart(string[] args)
        {
            UpdateSystemInfo();
            HeartBeatUpdateTimer.Start();
            NetworkInterfaceInformationUpdateTimer.Start();

        }

        protected override void OnStop()
        {
            HeartBeatUpdateTimer.Stop();
            NetworkInterfaceInformationUpdateTimer.Stop();
        }
    }
}
